package com.flashcash.solu.utils;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AlertDialog;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.flashcash.kotlin.utils.FirebaseAnalyticsConstants;
import com.flashcash.kotlin.utils.FirebaseAnalyticsUtils;
import com.flashcash.solu.R;
import com.flashcash.solu.bean.MarketApp;
import com.flashcash.solu.common.FieldParamsInt;
import com.x.leo.apphelper.data.cache.CacheDataManage;

import java.lang.reflect.Field;

/**
 * Created on 2019/4/29 下午8:06.
 */

public class CommonUtils {

    /**
     * 显示提示信息弹出框
     *
     * @param context
     * @param prevent  true为确定按钮在左，false为确定按钮在右
     * @param message
     * @param listener
     */
    public static void showMessageDialog(Context context, boolean prevent, String title, String message, DialogInterface.OnClickListener listener) {
        AlertDialog dialog = new AlertDialog.Builder(context)
                .setTitle(title)
                .setMessage(message)
                .setPositiveButton(prevent ? "Cancel" : "Confirm", prevent ? null : listener)
                .setNegativeButton(prevent ? "Confirm" : "Cancel", prevent ? listener : null).show();
//        setDialogCommonStyle(dialog, prevent);
    }

    /**
     * 设置dialog统一样式
     *
     * @param alertDialog
     * @param prevent     是否将确定按钮设置成灰
     */
    public static void setDialogCommonStyle(AlertDialog alertDialog, boolean prevent) {
        Button positive = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
        Button negative = alertDialog.getButton(AlertDialog.BUTTON_NEGATIVE);
//        positive.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
//        negative.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
//        positive.setTextColor(prevent ? ResourceUtils.getColorId(R.color.color_999999) : ResourceUtils.getColorId(R.color.colorMain));
//        negative.setTextColor(prevent ? ResourceUtils.getColorId(R.color.colorMain) : ResourceUtils.getColorId(R.color.color_999999));
        try {
            Field mAlert = AlertDialog.class.getDeclaredField("mAlert");
            mAlert.setAccessible(true);
            Object mAlertController = mAlert.get(alertDialog);
            //通过反射修改title字体大小和颜色
            Field mTitle = mAlertController.getClass().getDeclaredField("mTitleView");
            mTitle.setAccessible(true);
            TextView mTitleView = (TextView) mTitle.get(mAlertController);
//            mTitleView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 16);
//            mTitleView.setTextColor(ResourceUtils.getColorId(R.color.color_525666));
            //通过反射修改message字体大小和颜色
            Field mMessage = mAlertController.getClass().getDeclaredField("mMessageView");
            mMessage.setAccessible(true);
            TextView mMessageView = (TextView) mMessage.get(mAlertController);
//            mMessageView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 14);
//            mMessageView.setTextColor(ResourceUtils.getColorId(R.color.color_999999));
        } catch (IllegalAccessException e1) {
            e1.printStackTrace();
        } catch (NoSuchFieldException e2) {
            e2.printStackTrace();
        }
    }

    /**
     * 初始化可点击的图片
     *
     * @param view
     * @param context
     */
    public static void initMarketImage(View view, Context context) {
        MarketApp marketApp = CacheDataManage.INSTANCE.obtainData(FieldParamsInt.MARKET_APP, MarketApp.class);
        ImageView imageView = view.findViewById(R.id.iv_vpeso);
        if (marketApp != null && !TextUtils.isEmpty(marketApp.getLogo_name()) && !TextUtils.isEmpty(marketApp.getLoan_market_url())) {
            Glide.with(context)
                    .load(marketApp.getLogo_name())
                    .diskCacheStrategy(DiskCacheStrategy.ALL)
                    .into(imageView);
            if (marketApp.getLoan_market_url().contains("http") || marketApp.getLoan_market_url().contains("https")) {
                imageView.setOnClickListener(v -> {
                    FirebaseAnalyticsUtils.INSTANCE.logEvent(FirebaseAnalyticsConstants.Event.ACT_MORE_LOAN_AD, null);
                    Intent intent = new Intent();
                    intent.setAction("android.intent.action.VIEW");
                    Uri content_url = Uri.parse(marketApp.getLoan_market_url());
                    intent.setData(content_url);
                    context.startActivity(intent);
                });
            }
        } else {
            imageView.setVisibility(View.GONE);
        }
    }

    /**
     * 时间截转化成英文时间
     *
     * @param sourceTime
     * @return
     */
    public static String convertEnglishTime(String sourceTime) {
        String convertTime = TimeFormat.convertTime(sourceTime, "M d,yy");
        String englishTime = convertTime.split(" ")[1];
        switch (convertTime.split(" ")[0]) {
            case "1":
                englishTime = "Jan " + englishTime;
                break;
            case "2":
                englishTime = "Feb " + englishTime;
                break;
            case "3":
                englishTime = "Mar " + englishTime;
                break;
            case "4":
                englishTime = "Apr " + englishTime;
                break;
            case "5":
                englishTime = "May " + englishTime;
                break;
            case "6":
                englishTime = "Jun " + englishTime;
                break;
            case "7":
                englishTime = "Jul " + englishTime;
                break;
            case "8":
                englishTime = "Aug " + englishTime;
                break;
            case "9":
                englishTime = "Sept " + englishTime;
                break;
            case "10":
                englishTime = "Oct " + englishTime;
                break;
            case "11":
                englishTime = "Nov " + englishTime;
                break;
            case "12":
                englishTime = "Dec " + englishTime;
                break;
        }
        return englishTime;
    }

}
